﻿Imports CommandConsoleFramework

Public NotInheritable Class ExampleWorkerClass
    Protected Sub New()
        AddHandler CommandInterpreter.ProcessCommandLine, AddressOf ProcessCommandLine
    End Sub

    Private Sub ProcessCommandLine(sender As Object, e As ProcessCommandLineEventArgs)
        If Not e.HandledByCommandMethod Then
            If e.Line.CommandName = "+" Then
                Add(e.Line)
                e.HandledByCustom = True
            ElseIf e.Line.CommandName = "-" Then
                Subtract(e.Line)
                e.HandledByCustom = True
            ElseIf e.Line.CommandName = "?" Then
                e.Line.CommandName = "help"
                e.HandledByCommandMethod = True
            End If
        End If
    End Sub

    <CommandMethod("Displays a text file.", "This command will display an ASCII text file. However, since formatting is used, the file should not contain valid markup formatting commands unless you intend for them to be interpreted.")>
    <CommandMethodArgument("FilePath", 0, "The path and filename to open.")>
    Private Shared Sub OpenTextFile(line As CommandLine)
        Dim path As String = line.ArgumentString
        If System.IO.File.Exists(path) Then
            ConsoleOutput.WriteFormatted(System.IO.File.ReadAllText(path))
        Else
            line.SetResultMessage("File not found.", True)
        End If
    End Sub

    <CommandMethod("Sums the supplied numbers.", "Outputs the sum total of the numbers listed.")>
    <CommandMethodArgument("Numbers", 0, "One or more decimal numbers, seperated by a space.")>
    Private Shared Sub Add(line As CommandLine)
        If line.Words.Length > 1 Then
            Dim total As Decimal
            For index As Integer = 1 To line.Words.Length - 1
                Dim value As Decimal
                If Decimal.TryParse(line.Words(index).Word, value) Then
                    total += value
                Else
                    line.SetResultMessage("Invalid decimal number: " & line.Words(index).Word, True)
                    Exit For
                End If
            Next
            line.SetResultMessage(total, False)
        Else
            line.SetResultMessage("No numbers supplied.", True)
        End If
    End Sub

    <CommandMethod("Subtracts the supplied numbers.", "Outputs the result of subtracting the numbers listed.")>
    <CommandMethodArgument("Numbers", 0, "One or more decimal numbers, seperated by a space.")>
    Private Shared Sub Subtract(line As CommandLine)
        If line.Words.Length > 1 Then
            Dim total As Decimal
            For index As Integer = 1 To line.Words.Length - 1
                Dim value As Decimal
                If Decimal.TryParse(line.Words(index).Word, value) Then
                    If index = 1 Then
                        total = value
                    Else
                        total -= value
                    End If
                Else
                    line.SetResultMessage("Invalid decimal number: " & line.Words(index).Word, True)
                    Exit For
                End If
            Next
            line.SetResultMessage(total, False)
        Else
            line.SetResultMessage("No numbers supplied.", True)
        End If
    End Sub

    <CommandMethod()>
    Private Sub TestFoo(line As CommandLine)
        line.SetResultMessage("This was a local command.", False)
    End Sub
End Class